package lc;
import java.util.*;
import org.junit.*;
public class Ex1166 {
    class FileSystem {
        private Map<String, Integer> map;
        public FileSystem() {
            this.map = new HashMap<>();
        }

        public boolean create(String path, int val) {
            if (map.containsKey(path)) {
                return false;
            }
            List<String> list = paths(path);
            for (int i = 0; i < list.size() - 1; i++) {
                if (!map.containsKey(list.get(i))) {
                    return false;
                }
            }

            map.put(path, val);
            return true;
        }

        public int get(String path) {
            Integer res = map.get(path);
            if (res == null) {
                return -1;
            }
            return res;
        }

        private List<String> paths(String path) {
            List<String> res = new ArrayList<>();
            char[] cs = path.toCharArray();
            int start = 0;
            for (int i = 1; i < cs.length; i++) {
                if (cs[i] == '/') {
                    res.add(path.substring(start, i));
                    start = i;
                }
            }
            res.add(path.substring(start));
            return res;
        }
    }

    @Test
    public void test() {
        // ["FileSystem","create","create","get","create","get"]
        // [[],["/leet",1],["/leet/code",2],["/leet/code"],["/c/d",1],["/c"]]   
        FileSystem fs = new FileSystem();
        System.out.println(fs.create("/leet", 1));     
        System.out.println(fs.create("/leet/code", 2));    
        System.out.println(fs.get("/leet/code"));
        System.out.println(fs.create("/c/d",1));
        System.out.println(fs.get("/c"));
        System.out.println(fs.map);
    }
}
